<?php
	/**
	* @author: trungdt
	* Change logs
	*/
	/**
	*  {formhelper_list obj=$object keys='field_name,field_name' delimiter1="=" delimiter2=" " }
	*/

	function smarty_function_formhelper_list($params, &$smarty)
	{
		//$smarty->trigger_error("paging: missing 'current_page' parameter");
		//isset($params['page_length']

		$html="";
		$data = array();
		if (!isset($params['obj']))
		{
			$smarty->trigger_error("formhelper_list: missing 'obj' parameter");
		}
		else
		{
			$data = $params['obj'];
			$keys = array();
			$delimiter1 = " ";
			$delimiter2 = "=";

			if (isset($params['deli1']))
			{
				$delimiter1 = $params['deli1'];
			}
			if (isset($params['deli2']))
			{
				$delimiter2 = $params['deli2'];
			}

			if (isset($params['keys']))
			{
				$keys = explode(",",$params['keys']);
				foreach ($keys as $k=>$v)
					$keys[$k]=trim($v);
			}

			if (!is_array($data))
				$data=array($data);
			
			$print_sub_delim=false;
			if (count($data)>1)
				$print_sub_delim=true;
			$func_test = "function(";
			for ($i=0; $i<count($data); $i++)
			{
				$tdata = $data[$i];
				
				if ($print_sub_delim)
					$html.="{";
					
				if (!is_object($tdata))
					$t_data=(object)($tdata);
				
				foreach (get_object_vars($tdata) as $k=>$v)
				{
					if ((count($keys) > 0) && in_array($k,$keys))
						continue;

					if (!is_numeric($v))
					{
						// test is function or not
						$v=trim($v);
						if ((strlen($v) > strlen($func_test)) && (substr($v,0,strlen($func_test))==$func_test))
						{
							if (substr($v,strlen($func_test),3) == "___")
								// we force dont add "" for this item
								$v=substr($v,strlen($func_test)+3);
						}
						else if (!($v == "false" || $v == "true"))
						// format string
							$v="\"".str_replace("\"","\\\"",$v)."\"";
						//$v->$k1 = $v1;
					}
					$html.=" {$k}{$delimiter2}{$v} {$delimiter1}";
				}
				
				$html=substr($html,0,-strlen($delimiter1));
				
				if ($print_sub_delim)
					$html.="}";
				$html.=" {$delimiter1}";
				
			}
			$html=substr($html,0,-strlen($delimiter1));
		}
		return $html;
	}
?>